<template>

  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="teacherQuery.name" placeholder="讲师"/>
      </el-form-item>
      <el-form-item>
        <el-select v-model="teacherQuery.level" placeholder="头衔">
          <el-option label="高级讲师" value="1"/>
          <el-option label="首席讲师" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item label="入驻时间">
        <el-date-picker
          v-model="teacherQuery.joinDateBegin"
          value-format="yyyy-MM-dd"
          align="right"
          type="date"
          placeholder="开始时间"/>
        -
        <el-date-picker
          v-model="teacherQuery.joinDateEnd"
          value-format="yyyy-MM-dd"
          align="right"
          type="date"
          placeholder="结束时间"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="query">查询</el-button>
        <el-button type="default" @click="clear">清空</el-button>
        <!-- 批量删除 -->
        <el-button type="danger" style="width: 100px; margin-right:30px" @click="batchRemove">批量删除</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table :data="teachers" border stripe style="width: 100%;padding:15px" @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55"/>
      <el-table-column type="index" width="50px"/>
      <el-table-column label="头像" width="70px" align="center">
        <template slot-scope="scope">
          <img
            v-if="scope.row.avatar"
            :src="scope.row.avatar"
            style="width: 50px; height: 50px">
          <img
            v-else
            src="https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg"
            style="width: 50px; height: 50px">
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名字" width="80" />
      <el-table-column label="头衔" width="90">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.level === 1" type="success" size="mini">高级讲师</el-tag>
          <el-tag v-if="scope.row.level === 2" size="mini">首席讲师</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="intro" label="简介" />
      <el-table-column prop="sort" label="排序" width="60" />
      <el-table-column prop="joinDate" label="入驻时间" width="160" />
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页导航条 -->
    <el-pagination
      :current-page="pageNum"
      :page-sizes="[1,3,10,50]"
      :page-size="pageSize"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"/>
  </div>
</template>
<script>
import teacherApi from '@/api/teacher'
export default { // 相当于在vue对象内书写代码
  data() {
    return {
      teacherQuery: {}, // 讲师查询的条件对象
      teachers: [],
      pageNum: 1,
      pageSize: 3,
      total: 0,
      multipleSelection: []
    }
  },
  created() {
    // this.getTeacherList()
    this.getTeacherPage()
  },
  methods: {
    // 批量删除讲师
    batchRemove() {
      // id集合
      var ids = []
      for (var i = 0; i < this.multipleSelection.length; i++) { ids.push(this.multipleSelection[i].id) }
      if (ids.length === 0) { // 没有选中讲师
        this.$message.warning('请选择要删除的讲师')
        return
      }
      var idsStr = ids.join(',')
      // console.log('idStr' +idsStr)
      this.$confirm('此操作将永久删除记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        teacherApi.teacherBatchRemove(idsStr).then(response => {
          this.$message.success('批量删除成功')
          this.getTeacherPage()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 批量选择讲师
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    /**
     * 编辑讲师：
     *    1、点击list.vue页面中的编辑按钮，携带要编辑的讲师id到form.vue页面
     *    2、form.vue页面根据讲师id查询讲师信息.回显
     *    3、用户修改讲师信息.后点击提交按钮提交更新请求
     */

    // 编辑按钮单击事件
    handleEdit(index, teacher) {
      // console.log(index, teacher);
      this.$router.push(`/teacher/edit/${teacher.id}`)
    },
    // 删除单个讲师
    handleDelete(index, teacher) {
      this.$confirm(`此操作将永久删除${teacher.name}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        teacherApi.teacherRemoveById(teacher.id).then(response => {
          this.$message({
            type: 'success',
            message: response.message
          })
          this.getTeacherPage()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      // console.log(index, teacher);
    },
    clear() {
      // 1.删除查询条件
      this.teacherQuery = {}
      this.pageNum = 1
      this.pageSize = 3
      // 2.发起默认的请求
      this.getTeacherPage()
    },
    query() {
      this.getTeacherPage()
    },
    getTeacherPage() {
      teacherApi.teacherPage(this.pageNum, this.pageSize, this.teacherQuery).then(response => {
        console.log(response)
        this.teachers = response.data.page.records
        this.total = response.data.page.total
      })
    },
    getTeacherList() {
      teacherApi.teacherList().then(response => {
        console.log(response)
        this.teachers = response.data.items
      })
    },
    handleSizeChange(pageSize) {
      // 重新查询分页数据
      // console.log(`每页 ${val} 条`);
      this.pageSize = pageSize
      this.getTeacherPage()
    },
    handleCurrentChange(pageNum) {
      // 重新查询分页数据
      // console.log(`当前页: ${val}`);
      this.pageNum = pageNum
      this.getTeacherPage()
    }
  }
}
</script>
